/*
 * LunTotalStatistics.cpp
 *
 *  Created on: Dec 16, 2011
 *      Author: Netali
 */

#include "LunTotalStatistics.h"
#include <set>
#include <stdlib.h>
#include <string>
using namespace std;

string convertInt2(int number){
    if (number == 0)
        return "0";
    string temp="";
    string returnvalue="";
    while (number>0)
    {
        temp+=number%10+48;
        number/=10;
    }
    for (int i=0;i<(int)temp.length();i++)
        returnvalue+=temp[temp.length()-i-1];
    return returnvalue;
}

LunTotalStatistics::~LunTotalStatistics(){
	map<int,LunTimeStatistics*>::iterator it;
	for(it=my_stats->begin(); it!=my_stats->end(); it++){
		delete (*it).second;
	}
	delete my_stats;

	delete my_lun;
}

void LunTotalStatistics::addLunTimeStatistics(LunTimeStatistics* l){
	my_stats->insert(pair<int, LunTimeStatistics*>(l->getTime(), l));
}

LunTimeStatistics* LunTotalStatistics::getLunTimeStatistics(int time){
	if(my_stats->find(time) != my_stats->end())
		return my_stats->at(time);
	else
		return 0;
}

map<int,LunTimeStatistics*>* LunTotalStatistics::getAllLunTimeStatistics(){
	return my_stats;
}

Lun* LunTotalStatistics::getLun(){
	return my_lun;
}

string LunTotalStatistics::tostring(){
	string str = "";
	map<int,LunTimeStatistics*>::iterator myIterator;

	str.append("Lun ID: ");
	str.append(convertInt2(my_lun->getM_uid()));
	str.append("\n");
	for(myIterator = my_stats->begin(); myIterator != my_stats->end(); myIterator++)
	{
		str.append((*myIterator).second->toString());
		str.append("\n");
	}
	return str;
};

void LunTotalStatistics::startOver(){
	map<int,LunTimeStatistics*>::iterator it;
	for(it=my_stats->begin(); it!=my_stats->end(); it++){
		delete (it->second);
	}
	my_stats->clear();
}

